﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Excel;

namespace BYManageInfoSystem
{
    public partial class Frm_FactoryOutGoodsMoneyStat : DockContent
    {
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private static  DGBY.BLL.FinInvoiceRecordTab_New m_bllFinInvoiceRecordTab_New = new DGBY.BLL.FinInvoiceRecordTab_New();
        DataSet m_ds = new DataSet();
        String  m_strWhereCode;

        public Frm_FactoryOutGoodsMoneyStat()
        {
            InitializeComponent();
        }

        private void labelYear_Click(object sender, EventArgs e)
        {

        }

        private void textBoxYear_TextChanged(object sender, EventArgs e)
        {

        }

        private void labelMonth_Click(object sender, EventArgs e)
        {

        }

        private void textBoxMonth_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void InitCombox()
        {
            DataSet ds = m_bllBYCompanyTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxComp.DataSource = ds.Tables[0];
                this.comboBoxComp.DisplayMember = "经营单位简称";
                this.comboBoxComp.ValueMember = "BYCompanyTabID";
                this.comboBoxComp.SelectedIndex = 0;
            }

            ds = m_bllBYDepartTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDep.DataSource = ds.Tables[0];
                this.comboBoxDep.DisplayMember = "部门名称";
                this.comboBoxDep.ValueMember = "BYDepartTabID";
                this.comboBoxDep.SelectedIndex = 0;
            }
        }

        private void Frm_FactoryOutGoodsMoneyStat_Load(object sender, EventArgs e)
        {
            InitCombox();
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
        }

        private void buttonStat_Click(object sender, EventArgs e)
        {
            if (IsNull(this.errorProvider1, this.textHkRate))
            {
                return;
            }

            if (IsNull(this.errorProvider1, this.textDollorRate))
            {
                return;
            }

            StringBuilder strWhere = new StringBuilder();
            if (3 != Convert.ToInt32(this.comboBoxComp.SelectedValue.ToString()))
            {
                strWhere.Append(" BYCompanyTab.BYCompanyTabID = '" + this.comboBoxComp.SelectedValue + "'");
            }

            if (7 != Convert.ToInt32(this.comboBoxDep.SelectedValue.ToString()))
            {
                if (0 < strWhere.Length)
                {
                    strWhere.Append(" and ");
                }

                strWhere.Append(" BYDepartTab.BYDepartTabID = '" + this.comboBoxDep.SelectedValue + "'");
            }

            string strConditionTime = this.textBoxYear.Text + this.textBoxMonth.Text.PadLeft(2, '0') + " LIKE CONVERT(varchar(6), " + "case when Year(HxdNoTab.SBDate) = 1900 then HxdNoTab.TongJDate else HxdNoTab.SBDate end" + ", 112) ";
            if (0 < strWhere.Length)
            {
                strWhere.Append(" and ");
                strWhere.Append(strConditionTime);
            }
                                             
            ShowAllRecordInDataGridView(strWhere.ToString());
        }

        private void ShowAllRecordInDataGridView(String strWhere)
        {
            try
            {
                ultraGrid1.DataSource = null;
                m_ds = m_bllFinInvoiceRecordTab_New.GetFactoryOutGoodsMoneyStatInfo(strWhere, this.textHkRate.Text.ToString(), this.textDollorRate.Text.ToString());

                if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
                {
                    ultraGrid1.DataSource = m_ds.Tables[0];
                    ultraGrid1.DisplayLayout.Bands[0].Columns["是否港币出货工厂"].Hidden = true;
                    ultraGrid1.DisplayLayout.Bands[0].Columns["报关金额原值"].Hidden = true;

                    m_strWhereCode = strWhere;
                }
                else
                {
                    m_strWhereCode = "";
                }
            }
            catch { }
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            InitColumnWidth();
        }

        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂名称"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["货品名称"].Width = 160;
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关金额"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同金额"].Width = 100;
        }

        private void buttonExportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = (DataTable)ultraGrid1.DataSource;

                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                        SaveFileDialog sfd = new SaveFileDialog();
                        sfd.Filter = "Excel文件(*.xls)|*.xls";
                        string outFile = "";

                        if (sfd.ShowDialog() == DialogResult.OK)
                        {
                            outFile = sfd.FileName;
                        }
                        else
                        {
                            return;
                        }

                        ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                    }
                }
            }
            catch { }
        }

        private void ultraGridExcelExporter1_EndExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.EndExportEventArgs e)
        {
            try
            {
                Worksheet ws = e.CurrentWorksheet;
                int nCurrentRow = e.CurrentRowIndex;

                /******************************************************************************/
                //1.
                nCurrentRow += 3;
                ws.Rows[nCurrentRow].Cells[0].Value = "说明:1、运算汇率为当月财务公布的港币汇率:" + this.textHkRate.Text + "  及美元汇率:" + this.textDollorRate.Text;

                /******************************************************************************/
                //2.
                nCurrentRow++;
                ws.Rows[nCurrentRow].Cells[0].Value = "2、不以美元出口的业务都已折算成美元在上表反映，本月非美元出口的具体情况是：";

                DataTable dt = (DataTable)ultraGrid1.DataSource;
                int nCount = dt.Rows.Count;
                if (nCount > 0)
                {
                    for (int i = 0; i < nCount; i++)
                    {
                        if(Convert.ToBoolean(dt.Rows[i]["是否港币出货工厂"].ToString()))
                        {
                            nCurrentRow++;
                            ws.Rows[nCurrentRow].Cells[1].Value = dt.Rows[i]["工厂名称"].ToString() + "（HKD)：";
                            ws.Rows[nCurrentRow].Cells[2].Value = dt.Rows[i]["报关金额原值"].ToString();
                        }
                    }
                }

                /******************************************************************************/
                //3.
                nCurrentRow++;
                ws.Rows[nCurrentRow].Cells[0].Value = "3、";
                ws.Rows[nCurrentRow].Cells[1].Value = "以下工厂的价税合计数非确实，为用运算汇率计算得出";

                if (m_strWhereCode.Length > 0)
                {
                    m_ds = m_bllFinInvoiceRecordTab_New.GetFactoryTaxNotSure(m_strWhereCode);

                    if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
                    {
                        StringBuilder strFactory = new StringBuilder();
                        int nRowCount =  m_ds.Tables[0].Rows.Count;
                        for (int i = 0; i < nRowCount; i++)
                        {
                            strFactory.Append(m_ds.Tables[0].Rows[i]["工厂名称"].ToString());

                            if (i < nRowCount -1)
                                strFactory.Append("，");
                        }

                        nCurrentRow++;
                        ws.Rows[nCurrentRow].Cells[0].Value = strFactory;
                    }
                }
            }
            catch { }
        }

        private void ultraGridExcelExporter1_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e)
        {

        }

        public bool IsNull(ErrorProvider errorProvider, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsNum(TextBox.Text) && !IsDecimal(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        private bool IsDecimal(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"([1-9]+[0-9]*(\.[0-9]+))|(0(\.[0-9]+))");
            return bRet;
        }
    }
}
